package edu.wpi.cs509.ui.simulators;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import edu.wpi.cs509.clientlogic.UserSession;
import edu.wpi.cs509.common.exceptions.IllegalMove;
import edu.wpi.cs509.common.exceptions.InitialPlacementError;
import edu.wpi.cs509.common.exceptions.InvalidLocation;
import edu.wpi.cs509.common.exceptions.ServerUnreachable;
import edu.wpi.cs509.representation.GameListItem;
import edu.wpi.cs509.representation.Location;
import edu.wpi.cs509.representation.PlayerPiece;
import edu.wpi.cs509.representation.StrategoPiece;
import edu.wpi.cs509.representation.TeamColor;
import edu.wpi.cs509.ui.intfc.ClientLogicNotify;
import edu.wpi.cs509.ui.intfc.GameInterface;
import edu.wpi.cs509.ui.intfc.PlayerStatsInterface;
import edu.wpi.cs509.ui.intfc.RulesInterface;

public class SimClientLogicNotifyHandler implements ClientLogicNotify {

	@Override
	public void notifyLoginSuccess(UserSession session) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void notifyLeaderBoard(List<PlayerStatsInterface> statsList,
			int rank, int totalPlayers) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void notifyPlayerStats(PlayerStatsInterface myStats) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void notifyGames(List<GameListItem> games) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void notifyGameJoined(GameInterface game) {
		// when the simulator joins, it places it's pieces automatically and readies up
		// build a list of all my pieces
		List<PlayerPiece> list = new ArrayList<PlayerPiece>();
		for (StrategoPiece p : StrategoPiece.ALL_GAME_PIECES) {
			for (int i=0; i<p.numberPerUser(); i++ ) {
				list.add( new PlayerPiece( p, game.getMyColor() ) );
			}
		}
		
		Iterator<PlayerPiece> itr = list.iterator();
		
		int startRow = 0;
		if (game.getMyColor() == TeamColor.BLUE) {
			startRow = 6;
		}
			
		for (int x=0; x<10; x++) {
			for (int y=startRow; y<(startRow+4); y++) {
				try {
					game.placePiece(new Location(x,y), itr.next());
				} catch (IllegalMove e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvalidLocation e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ServerUnreachable e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

		// Notify ready
		try {
			game.ready();
		} catch (InitialPlacementError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServerUnreachable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void notifyGameObserved(GameInterface game) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void notifyRulesPage(RulesInterface rules) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void notifyFailure(FailureReason reason) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ServerConnectionLost() {
		// TODO Auto-generated method stub
		
	}

}
